package com.goodflower.goodflower;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.goodflower.goodflower.address.Address;
import com.goodflower.goodflower.info.Info;
import com.goodflower.goodflower.user_register.User_register_info;
import com.goodflower.goodflower.util.HttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private Button btn_login;//登录按钮
    private Button btn_zhuce;//注册按钮
    private EditText username;//用户名
    private EditText password;//密码
    private ProgressDialog login_pg;//登录时后的请求
    private CheckBox checkbox_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_view);

        btn_login = (Button)findViewById(R.id.btn_login);
        btn_zhuce = (Button)findViewById(R.id.btn_zhuce);
        username = (EditText)findViewById(R.id.my_name);
        password = (EditText)findViewById(R.id.my_password);
        checkbox_login = (CheckBox)findViewById(R.id.checkbox_login);
        login_pg = new ProgressDialog(this);
        login_pg.setMessage("正在登陆,请稍后");

        SharedPreferences login_date = getSharedPreferences("login_date", MODE_PRIVATE);

        String login_name = login_date.getString("login_name", "");
        String login_phone = login_date.getString("phone","");
        if(!login_name.isEmpty()){
            Info.current_user_name = login_name;
            Info.current_user_phone = login_phone;
            Intent intent = new Intent(MainActivity.this, FlowerActivity.class);
            intent.putExtra("yourname",login_name);
            startActivity(intent);
            finish();
            overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
        }

        SharedPreferences sharedPreferences = getSharedPreferences("date", MODE_PRIVATE);
        String tishi = sharedPreferences.getString("tishi", null);
        if(tishi==null){
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("tishi","0");
            editor.apply();
        }

        btn_zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, User_register_info.class));
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View v) {
                login_pg.show();
                Log.d("ceshi",Address.ADDRESS_LOGIN+"name="+username.getText()+"&password="+password.getText());
                HttpUtil.sendOkHttpRequest(Address.ADDRESS_LOGIN+"name="+username.getText()+"&password="+password.getText(), new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(MainActivity.this,"网络请求失败,请检查网络设置",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        /********测试专用*********/
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        /****************/
                        login_pg.dismiss();

                        String login_response = response.body().string();
                        String[] split = login_response.split("\\|");
                        String  httpresponse = split[0];
                        String phone = split[1];
                        if(httpresponse.equals("ok")){
                            Log.d("login","登陆成功");
                            if(checkbox_login.isChecked()){
                                SharedPreferences.Editor editor = getSharedPreferences("login_date", MODE_PRIVATE).edit();
                                editor.putString("login_name",username.getText().toString());
                                editor.putString("phone",phone);
                                editor.apply();
                            }
                            Info.current_user_name = username.getText().toString();
                            Info.current_user_phone = phone;
                            Intent intent = new Intent(MainActivity.this, FlowerActivity.class);
                            intent.putExtra("yourname",username.getText().toString());
                            startActivity(intent);
                            finish();
                            overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);

                        }
                        else {
                            Looper.prepare();

                            AlertDialog.Builder login_false = new AlertDialog.Builder(MainActivity.this);
                            login_false.setTitle("登录失败");
                            login_false.setMessage("请检查您的账号和密码");
                            login_false.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                            login_false.show();
                            Looper.loop();
                        }
                    }
                });
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK)
            return true;//不执行父类点击事件

        return super.onKeyDown(keyCode, event);
    }
}
